$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/fyaddressbook/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '好友名字', name: 'friendName', index: 'friend_name', width: 80 }, 			
			{ label: '手机号', name: 'phoneNumber', index: 'phone_number', width: 80 }, 			
			{ label: '关联用户', name: 'userId', index: 'user_id', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var username ;
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/fyusers/info/"+rowdata.userId,
                        success: function(r){
                            username = r.fyUsers.userName;

                        }
                    });
                    return username;
                } },
			{ label: '字母类型', name: 'letterType', index: 'letter_type', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            username: null
        },
		showList: true,
		title: null,
		fyAddressBook: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.fyAddressBook = {};
            vm.getUser("add");
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            vm.getUser("update");
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.fyAddressBook.id == null ? "sys/fyaddressbook/save" : "sys/fyaddressbook/update";
			if(vm.fyAddressBook.friendName == ''||typeof vm.fyAddressBook.friendName=='undefined'){
				alert("好友名字不能为空");
			}else if(vm.fyAddressBook.phoneNumber == ''||typeof vm.fyAddressBook.phoneNumber =='undefined'){
                alert("好友手机号码不能为空");
			}else if(!(/^1(3|4|5|7|8)\d{9}$/.test(vm.fyAddressBook.phoneNumber))){
                alert("手机号码有误，请重填");
            }else{
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.fyAddressBook),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
			}

		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/fyaddressbook/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/fyaddressbook/info/"+id, function(r){
                vm.fyAddressBook = r.fyAddressBook;
            });
		},
        getUser: function(mn){
            $("#userid").html("");
            $.get(baseURL + "sys/fyusers/alllist", function(r){
                var temp;
                $.each(r.alllist, function (i) {
                    if(mn =='add' && i ==0){
                        vm.fyAddressBook.userId = r.alllist[i].userId;
                    }
                    temp +="<option value='"+r.alllist[i].userId+"'>"+r.alllist[i].userName+"</option>";

                });
                $("#userid").append(temp);

            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'username': vm.q.username},
                page:page
            }).trigger("reloadGrid");
		}
	}
});